
//
// 120125 - AH - Created.
// 120205 - AH - (Bien) Removed some debug printout.
//

package org.soekrat.engine.motor.passingstrategies;

import java.util.List;

import org.soekrat.engine.SoekratEngine;
import org.soekrat.engine.motor.ConversationElement;
import org.soekrat.engine.motor.Motor;
import org.soekrat.engine.wheel.Wheel;
import org.soekrat.engine.wheel.ontology.Ontology;
import org.soekrat.engine.wheel.retrieval.RetrievalEvent;
import org.soekrat.engine.wheel.retrieval.RetrievalEventType;

/**
 * <p>
 *   {@link PassingStrategy} that defines a sequence of 
 *   {@link Ontology} 
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class WheelPassingStrategy extends SequenceBasedPassingStrategy 
{
  private int m_turns=1;
  
  private int m_pos=0;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public WheelPassingStrategy(){}
  
  /**
   * <p>
   *   Sets turns.
   * </p>
   * @param n Turn count.
   * @throws IllegalArgumentException If {@code n<1}.
   */
  public void setTurns(int n)
  {
    if (n<1)
    {
      throw new IllegalArgumentException("<n");
    }
    
    m_turns=n;
    m_turn=-1;
  }
  
  /**
   * <p>
   *   Gets number of turns.
   * </p>
   * @return Count.
   */
  public int getTurns(){ return m_turns; }
  
  @Override
  public void setSequence(Wheel wheel)
  {
    super.setSequence(wheel);
    setTurns(m_turns);
  }
  
  @Override
  public void setSequence(List<String> names)
  {
    super.setSequence(names);
    setTurns(m_turns);
  }
  
  private int m_turn=-1;
  
  @Override
  public Ontology getOntology(Motor motor, ConversationElement ce, 
    RetrievalEvent re) 
  {
    if (ce==null)
    {
      throw new NullPointerException("ce");
    }
    
    if (m_turn==-1)
    {
      m_turn=0;
      re=new RetrievalEvent(re, "Turn "+m_turn, RetrievalEventType.TURN, null);
      re.setTurn(m_turn);
    }
    
    if (m_pos>=m_seq.size())
    {
      if (m_turn<m_turns-1)
      {
        m_pos=0;
        m_turn++;
        re=new RetrievalEvent(re, "Turn "+m_turn, RetrievalEventType.TURN, 
          null);
        re.setTurn(m_turn);
      }
      else
      {
        return null;
      }
    }
    
    String name=m_seq.get(m_pos++);
    Wheel wheel=motor.getWheel();
    
    Ontology retVal=wheel.getOntology(name);
    
    return retVal;
  }

  @Override
  public void reset(){ m_pos=0; setTurns(m_turns); }
  
  @Override
  public String toString() 
  {
    String txt=super.toString();
    StringBuilder sb=new StringBuilder();
    sb.append("number-of-turns=").append(m_turns).append(", ").append(txt);
    return sb.toString();
  }
  
  public static void main(String[] args) 
  {
    RetrievalEvent re=new RetrievalEvent();
    
    SoekratEngine engine=new SoekratEngine();
    Motor motor=engine.getMotor();
    ConversationElement ce=new ConversationElement();
    
    WheelPassingStrategy sps=new WheelPassingStrategy();
    sps.setSequence(motor.getWheel());
    sps.setTurns(2);
    
    System.out.println(sps);
    
    while (true)
    {
      Ontology spoke=sps.getOntology(motor, ce, re);
      if (spoke==null)
      {
        System.out.println("done");
        System.exit(0);
      }
      else
      {
        System.out.println(spoke.getName());
      }
    }
  }
  
}
